﻿using System;
using System.Collections.Generic;

using System.Web;
using System.IO;
using MYLSOFT.JsonSimple;
using com.byt.webcore;
using com.byt.webcore.utils;
using com.byt.suce.db.mapper;
using com.byt.suce.db.factory;
using com.byt.suce.db.dao;

namespace suce.upload
{
    /// <summary>
    /// Summary description for Upload
    /// </summary>
    public class Upload : IHttpHandler
    {

        private IPkgSolicitud DAO = PkgSolicitudFactory.create();

        private string GetParam(HttpRequest req, string name)
        {
            string value = null;
            try
            {
                value = req.Form[name];
                if (string.IsNullOrEmpty(value))
                    value = req.Params[name];

            }
            catch
            {
                return null;
            }
            return value;
        }

        public void ProcessRequest(HttpContext context)
        {
            UploadHelper.pathFiles = AppConfig.getPath(null, "upload");

            HttpRequest req = context.Request;

            string command = GetParam(req, "C");
            if (!string.IsNullOrEmpty(command))
            {
                Procesar(req, context.Response, command);
                return;
            }

            // UPLOAD
            string numesolicitud = GetParam(req, "numesolicitud");
            string folder = GetParam(req, "folder");
            string dir = UploadHelper.GetPath(numesolicitud, folder, null);
            HttpPostedFile uploadFile;
            JSONArray ja = new JSONArray();
            JSONObject jo;
            for (int i = 0; i < req.Files.Count; i++)
            {
                uploadFile = req.Files[i];
                if (!string.IsNullOrEmpty(uploadFile.FileName))
                {
                    uploadFile.SaveAs(Path.Combine(dir, uploadFile.FileName));
                }
            }
            jo = new JSONObject();
            jo.Add("success", 1);
            context.Response.Write(jo.ToString());
        }

        private void Procesar(HttpRequest req, HttpResponse resp, string command)
        {
            string numesolicitud = GetParam(req, "numesolicitud");
            string folder = GetParam(req, "folder");

            if ("GETFILES".Equals(command))
            {
                resp.Write(UploadHelper.ListFiles(Path.Combine(numesolicitud, folder)));
            }
            else if ("DELETE".Equals(command))
            {
                string filename = GetParam(req, "filename");
                File.Delete(UploadHelper.GetPath(numesolicitud, folder, filename));

            }
            else if ("DELETEALL".Equals(command))
            {
                String path = UploadHelper.GetPath(numesolicitud, folder, String.Empty)+ '/';
                UploadHelper.deleteAll(path);

            }
        }

        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}